package com.shenluyao.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @date 2019-11-19
 */
public class CookiesCommon {
    public CookiesCommon() {
    }

    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }

        response.addCookie(cookie);
    }

    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            Cookie cookie = (Cookie)cookieMap.get(name);
            return cookie;
        } else {
            return null;
        }
    }

    public static String getCookieByNameValue(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            Cookie cookie = (Cookie)cookieMap.get(name);
            return cookie.getValue();
        } else {
            return null;
        }
    }

    private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap();
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Cookie[] var6 = cookies;
            int var5 = cookies.length;

            for(int var4 = 0; var4 < var5; ++var4) {
                Cookie cookie = var6[var4];
                cookieMap.put(cookie.getName(), cookie);
            }
        }

        return cookieMap;
    }

    public static void cancleCookie(HttpServletRequest request, HttpServletResponse response, String name, String domain) {
        Cookie cookie = new Cookie(name, "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        if (domain != null && !domain.equals("")) {
            cookie.setDomain(domain);
        }

        response.addCookie(cookie);
    }

    public static void cancleAllCookie(HttpServletRequest request, HttpServletResponse response, String domain) {
        Cookie[] cookies = request.getCookies();
        int i = 0;

        for(int len = cookies.length; i < len; ++i) {
            Cookie cookie = new Cookie(cookies[i].getName(), (String)null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
            if (domain != null && !domain.equals("")) {
                cookie.setDomain(domain);
            }

            response.addCookie(cookie);
            Cookie cookie1 = new Cookie(cookies[i].getName(), (String)null);
            cookie1.setMaxAge(0);
            cookie1.setPath("/");
            cookie1.setDomain("www.htxxpx.com");
            response.addCookie(cookie1);
            Cookie cookie2 = new Cookie(cookies[i].getName(), (String)null);
            cookie2.setMaxAge(0);
            cookie2.setPath("/");
            cookie2.setDomain("hxcbs.htxxpx.com");
            response.addCookie(cookie2);
        }

    }
}
